<?php

if (!defined('WHATSIT_PATH')) {
    require_once('../lib/config.php');
}
require_once(WHATSIT_PATH.'/lib/JSON.php');

if (array_key_exists('id', $_GET)) {
    $id = (int)$_GET['id'];

    if (array_key_exists('view', $_GET)) {
	$view = $_GET['view'];
    } else {
	$view = 'home';
    }
    
    $user = new $whatsit_user_class();
    $db = new $whatsit_database_class($user->getUserName());
    
    $whatsit = $db->getWhatsit($id);

    if (!($whatsit instanceof Whatsit_Interface) && array_key_exists('url', $_GET)) {
        $whatsit = new Whatsit(1, $_GET['url']);
    }
    
    $pref_array = $whatsit->getPreferenceArray();
        
    $json = new Services_JSON();
    $json_prefs = $json->encode($pref_array);
} else {
    die('couldnt find widget id');
    $json_prefs = '';
}
?>
<html>
    <head>
        <title><?= $whatsit->getTitle() ?></title>
        <script type="text/javascript">
            var module_id = <?= $whatsit->getID() ?>;
            var initial_prefs = <?= $json_prefs ?>;
	    var current_view = '<?= $view ?>';
        </script>
        <script src="<?= WHATSIT_URL_PATH ?>/javascript/lib.js" type="text/javascript"></script>
        <?php foreach ($whatsit->getRequiredFeatures() as $feature) : ?>
        <?php if (is_file(WHATSIT_PATH."/javascript/$feature.js")) : ?>
        <script src="<?= WHATSIT_URL_PATH ?>/javascript/<?= $feature ?>.js"  type="text/javascript"></script>
        <?php else: ?>
        <!-- ignoring <?= WHATSIT_URL_PATH ?>/javascript/<?= $feature ?>.js because <?= WHATSIT_PATH."/javascript/<?= $feature ?>.js" ?> doesn't exist-->
        <?php endif; ?>
        <?php if (file_exists(WHATSIT_PATH."/css/$feature.css")) : ?>
        <script src="<?= WHATSIT_URL_PATH ?>/css/<?= $feature ?>.css"  type="text/javascript"></script>
        <?php endif; ?>
        <?php endforeach; ?>
    </head>
    <body onload="whatsits_onloads();">
        <div id="content-<?= $whatsit->getID() ?>" style="border:0;padding:0;margin:0;width:100%;height:auto;overflow:hidden;">
        <?php $whatsit->drawContent($view); ?>
        </div>
    </body>
</html>